package heliotrope.game.system
{
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	
	import heliotrope.game.GameVar;
	import heliotrope.game.character.Player;
	import heliotrope.game.scene.map.Map;
	import heliotrope.global.GlobalEventScheduler;
	import heliotrope.global.GlobalVar;

	public class MoveSystem
	{
		private static var __instance:MoveSystem;
		private var _moveSpeed:uint = 10;
		private var _moveInfo:MoveInfo;

		private var _gameVar:GameVar;
		private var _moveListeners:Dictionary;
		public function MoveSystem(pvt:PrivateClass)
		{
			_moveInfo = new MoveInfo;
			_gameVar = GameVar.getInstance();
		}
		public function moveTo(x_:Number, y_:Number):void
		{
			
		}
		public function setPlayerPos(x_:Number, y_:Number):void
		{
			_gameVar.playerPos.setTo(x_, y_);
			RenderSystem.getInstance().forceSet = true;
		}
		
		private function moveInit():void
		{
			var distanceX:Number;
			var distanceY:Number;
			var distance:Point = _moveInfo.des.subtract(_gameVar.playerPos);
			if(distance != 0)
			{
				_moveInfo.movSpeedX = _moveSpeed*distance.x/distance.length;
				_moveInfo.movSpeedY = _moveSpeed*distance.y/distance.length;
			}
			else
			{
				_moveInfo.movSpeedX = _moveInfo.movSpeedY = 0;
			}
		}
		
		private function updateHandler(event:Event):void
		{
			var detaX:Number = Math.abs(_gameVar.playerPos.x - _moveInfo.desX);
			var detaY:Number = Math.abs(_gameVar.playerPos.y - _moveInfo.desY);
			var flag:int = 0;
			var newX:Number = detaX<=Math.abs(_moveInfo.movSpeedX)?(flag |= 0x1,_moveInfo.desX) : _gameVar.playerPos.x+_moveInfo.movSpeedX;
			var newY:Number = detaY<=Math.abs(_moveInfo.movSpeedY)?(flag |= 0x2,_moveInfo.desY) : _gameVar.playerPos.y+_moveInfo.movSpeedY;
			
			_gameVar.playerPos.setTo(newX, newY);
			if(flag == 0x3)
			{
				_moveInfo.des = null;//_walkArray.shift();
				if(!_moveInfo.des)
				{
					GlobalEventScheduler.getInstance().removeEnterFrameListener(updateHandler);
					return;
				}
				moveInit();
			}
		}
		
		public static function init():void
		{
			if(MoveSystem.__instance)
			{
				throw new Error("You can't init the MoveSystem twice!")
			}
			MoveSystem.__instance = new MoveSystem(new PrivateClass);
		}
		
		public static function getInstance():MoveSystem
		{
			return MoveSystem.__instance;
		}
	}
}
import flash.geom.Point;
class MoveInfo
{
	private var _des:Point;
	public var desX:uint;
	public var desY:uint;
	public var movSpeedX:uint;
	public var movSpeedY:uint;
	public var moveArray:Array;
	public function MoveInfo()
	{
		_des = new Point;
	}

	public function get des():Point{return _des;}
	public function set des(value_:Point):void{desX=value_.x,desY=value_.y;_des = value_;}

}
class PrivateClass
{
	public function PrivateClass()
	{
	}
}